<template>
  <div id="box">
    <h3>Form表单</h3>
    <hr>
    <div style="margin: 20px;"></div>
<!--
  :model="formLabelAlign"   绑定表单收集数据存储的对象
  Form 组件提供了表单验证的功能，只需要通过 rules 属性传入约定的验证规则，
  并将 Form-Item 的 prop 属性设置为需校验的字段名即可

  ref="ruleForm"  是获取节点对象的标识
-->
    <div id="clear"> <el-button @click="clearInfo">清空校验提示</el-button></div>
    <el-form :label-position="labelPosition" :rules="rules" ref="ruleForm"  label-width="80px" :model="formLabelAlign">
      <el-form-item label="用户名" prop="name">
        <el-input v-model="formLabelAlign.name"></el-input>
      </el-form-item>
      <el-form-item label="活动区域" prop="region">
        <el-input v-model="formLabelAlign.region"></el-input>
      </el-form-item>
      <el-form-item label="活动形式" prop="type">
        <el-input v-model="formLabelAlign.type"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain  @click="submit('ruleForm')">提交</el-button>
      </el-form-item>
    </el-form>
    <hr>
    <h3>自定义校验规则</h3>
    <el-form :model="ruleForm" status-icon ref="ruleForm2" :rules="rules" label-width="100px" class="demo-ruleForm">
      <el-form-item label="新密码" prop="pass">
        <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetForm('ruleForm2')">重置</el-button>
      </el-form-item>
    </el-form>



  </div>
</template>

<script>
export default {
  name: "FormView",
  data() {
    // 自定义规则1
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm2.validateField('checkPass');
        }
        // 满足要求
        callback();
      }
    };

    // 自定义规则2
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };

    return {
      //  调整label位置
      labelPosition: 'left',
      formLabelAlign: {
        name: '',
        region: '',
        type: ''
      },
      ruleForm:{
        pass:'',
        checkPass:''
      },
      rules:{
        name:[
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        region:[
          { required: true, message: '请输入活动区域', trigger: 'blur' }
        ],
        type:[
          { required: true, message: '请输入活动类型', trigger: 'blur' }
        ],
        pass:[
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],

      }
    }
  },
  methods:{
    submit(formName){
      // 发送http请求之前  校验表单规则    js   this.$refs  获取当前组件中所有含有ref属性的节点对象
      //  validate(valid=>{})   表单校验通过  函数参数是true   否则传入false
      this.$refs[formName].validate( valid => {
        if (valid) {
          console.log("http请求已发送...",JSON.stringify(this.formLabelAlign))
        } else {
             console.log('请填写正确的数据');
          return false;
        }
      });

    },
    clearInfo(){
      // 清空校验信息
      this.$refs.ruleForm.clearValidate()
    },
    resetForm(formName){
      this.$refs[formName].resetFields();
    }
  }
}


</script>

<style scoped>

#box {
  width: 80%;
  height: 400px;
  margin: 100px auto;
}

#clear{
  margin-bottom: 20px;
}
</style>
